package com.brokendust.client.api.util.image;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.util.ResourceLocation;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

public class StringToImage {
    public static HashMap<RenderingHints.Key, Object> RenderingProperties;

    static {
        (RenderingProperties = new HashMap<RenderingHints.Key, Object>()).put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        StringToImage.RenderingProperties.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        StringToImage.RenderingProperties.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    }

    public static BufferedImage textToImage(String Text, Font f, float Size) {
        f = f.deriveFont(Size);
        FontRenderContext frc = new FontRenderContext(null, true, true);
        LineMetrics lm = f.getLineMetrics(Text, frc);
        Rectangle2D r2d = f.getStringBounds(Text, frc);
        BufferedImage img = new BufferedImage((int) Math.ceil(r2d.getWidth()), (int) Math.ceil(r2d.getHeight()), 2);
        Graphics2D g2d = img.createGraphics();
        g2d.setRenderingHints(StringToImage.RenderingProperties);
        g2d.setBackground(Color.WHITE);
        g2d.setColor(Color.BLACK);
        g2d.clearRect(0, 0, img.getWidth(), img.getHeight());
        g2d.setFont(f);
        g2d.drawString(Text, 0.0f, lm.getAscent());
        g2d.dispose();
        return img;
    }

    public static ResourceLocation test(String display) {
        String text = display;
        BufferedImage image = new BufferedImage(1, 1, 2);
        Graphics2D graphics2d = image.createGraphics();
        Font font = new Font("TimesNewRoman", 1, 24);
        graphics2d.setFont(font);
        FontMetrics fontmetrics = graphics2d.getFontMetrics();
        int width = fontmetrics.stringWidth(text);
        int height = fontmetrics.getHeight();
        graphics2d.dispose();
        image = new BufferedImage(width, height, 2);
        graphics2d = image.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        graphics2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        graphics2d.setFont(font);
        fontmetrics = graphics2d.getFontMetrics();
        graphics2d.setColor(Color.GREEN);
        graphics2d.drawString(text, 0, fontmetrics.getAscent());
        graphics2d.dispose();
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "png", out);
            NativeImage nativeImage = NativeImage.read(new ByteArrayInputStream(out.toByteArray(), 0, out.size()));
            DynamicTexture dynamicTexture = new DynamicTexture(nativeImage);
            return Minecraft.getInstance().getTextureManager().register("menu", dynamicTexture);
        } catch (IOException ex) {
            return new ResourceLocation("textures/gui/demo_background.png");
        }
    }
}
